package com.houtai.service.campus;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TdCampus;
import com.houtai.bean.dto.campus.TdCampusListDto;
import com.houtai.common.util.LogUtils;
import com.houtai.common.util.MD5;
import com.houtai.dao.TdCampusDao;
import com.houtai.dao.TsJzAdminOperationDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2017/7/28.
 */
@Service

public class CampusService {
    private static Logger logger = Logger.getLogger(CampusService.class);
    @Autowired
    private TdCampusDao tdCampusDao;


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * @Purpose   查找园区
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findTdCampusList(TdCampusListDto page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findTdCampusList:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "campus_id desc");
            List<TdCampus> lists = tdCampusDao.findTdCampusList(page);
            PageInfo<TdCampus> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("campus_name", page.getCampus_name());
            map.put("campus_type", page.getCampus_type());
            map.put("state", page.getState());
            map.put("province_id", page.getProvince_id());
            map.put("city_id", page.getCity_id());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找园区" , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
    public List<TdCampus> findAllTdCampus() {
        TdCampusListDto dto = new TdCampusListDto();
        return tdCampusDao.findTdCampusList(dto);
    }
    /**
     * @Purpose   添加园区
     * @param     TdCampus
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public synchronized RetInfo  insertTdCampus(TdCampus TdCampus, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":insertTdCampus:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdCampus acount = tdCampusDao.findTdCampusByAccount(TdCampus.getCampus_account());
            if (acount != null) {
                retInfo.setMark("1");
                retInfo.setTip("该登录账号已存在");
            } else {
                TdCampus.setSet_up_time(format.parse(TdCampus.getSet_up_time_s()));
                TdCampus.setCampus_psd(MD5.code(TdCampus.getCampus_psd()));
                tdCampusDao.insertTdCampus(TdCampus);
                retInfo.setMark("0");
                retInfo.setTip("创建成功.");
                LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加园区,id:" + TdCampus.getCampus_id(),request);
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   修改园区
     * @param     TdCampus
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public synchronized RetInfo updateTdCampus(TdCampus TdCampus, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateTdCampus:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdCampus acount = tdCampusDao.findTdCampusByAccount(TdCampus.getCampus_account());
            if (acount != null) {
                if (acount.getCampus_id() == TdCampus.getCampus_id()) {
                    TdCampus.setSet_up_time(format.parse(TdCampus.getSet_up_time_s()));
                    tdCampusDao.updateTdCampus(TdCampus);
                    retInfo.setMark("0");
                    retInfo.setTip("成功.");
                    LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改园区,id:" + TdCampus.getCampus_id(),request);
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("该登录账号已存在");
                }

            } else {
                TdCampus.setSet_up_time(format.parse(TdCampus.getSet_up_time_s()));
                tdCampusDao.updateTdCampus(TdCampus);
                retInfo.setMark("0");
                retInfo.setTip("成功.");
                LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改园区,id:" + TdCampus.getCampus_id(),request);
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   删除园区
     * @param     campus_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo deleteTdCampus(int campus_id, int state, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":deleteTdCampus:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdCampusDao.updateTdCampusState(campus_id, state);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改园区状态,id:" + campus_id
                     + "状态：" + state,request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   重置园区密码
     * @param     campus_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo updateTdCampusPsd(int campus_id, String campus_psd, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateTdCampusPsd:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {

            tdCampusDao.updateTdCampusPsd(campus_id, MD5.code(campus_psd));
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"重置园区密码,id:" + campus_id,request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public TdCampus findTdCampusById(Integer campus_id) {
        return tdCampusDao.findTdCampusDetail(campus_id);
    }

}
